package org.balabala.os.conf;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;


public class ObjectStorageConf {

    public String endpoint;

    public String bucketName;

    public String accessKey;

    public String secretKey;

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public ObjectStorageConf(InputStream confInputStream) {
        Map<String, Object> properties;
        Map<String, String> MinioProperties;
        properties = new Yaml().loadAs(confInputStream, Map.class);
        MinioProperties = (Map<String, String>) properties.get("MinIO");
        this.bucketName = MinioProperties.get("bucketName");
        this.endpoint = MinioProperties.get("endpoint");
        this.accessKey = MinioProperties.get("accessKey");
        this.secretKey = MinioProperties.get("secretKey");
    }

    public ObjectStorageConf(File confFile) throws FileNotFoundException {
        this(new FileInputStream(confFile));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ObjectStorageConf that = (ObjectStorageConf) o;

        if (!Objects.equals(endpoint, that.endpoint)) return false;
        if (!Objects.equals(bucketName, that.bucketName)) return false;
        if (!Objects.equals(accessKey, that.accessKey)) return false;
        return Objects.equals(secretKey, that.secretKey);
    }

    @Override
    public int hashCode() {
        int result = endpoint != null ? endpoint.hashCode() : 0;
        result = 31 * result + (bucketName != null ? bucketName.hashCode() : 0);
        result = 31 * result + (accessKey != null ? accessKey.hashCode() : 0);
        result = 31 * result + (secretKey != null ? secretKey.hashCode() : 0);
        return result;
    }
}
